package com.work.blog.interceptor;

import com.work.blog.constant.Constant;
import com.work.blog.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:DELL
 * Date:2025-06-17
 * Time:17:16
 */
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //1.获取token---header由key，value构成
        String token = request.getHeader(Constant.REQUEST_HEADER_TOKEN);
        log.info("从header中获取token：{}",token);
        //2.校验token是否放行
        Claims claims = JwtUtils.parseToken(token);//解析
        if (claims==null){
            //校验失败
            response.setStatus(401);
            return false;
        }else {
            return true;
        }
    }
}
